package com.yanxinim.cn.ui.me.redpacket;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.xuan.xuanhttplibrary.okhttp.callback.ListCallback;
import com.xuan.xuanhttplibrary.okhttp.result.ArrayResult;
import com.yanxinim.cn.R;
import com.yanxinim.cn.adapter.MySubordinateAdapter;
import com.yanxinim.cn.adapter.WithdrawRecordAdapter;
import com.yanxinim.cn.bean.redpacket.WithdrawRecordBean;
import com.yanxinim.cn.ui.base.BaseActivity;
import com.yanxinim.cn.ui.me.MySubordinateActivity;
import com.yanxinim.cn.util.ToastUtil;
import com.yanxinim.cn.view.MyListView;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import okhttp3.Call;

/**
 * @ClassName WithdrawRecordActivity
 * @Description 提现记录
 * @Author ChenXL
 * @Date 2021/5/29 11:15
 * @Version 1.0
 */
public class WithdrawRecordActivity extends BaseActivity {

    private SmartRefreshLayout mRefreshLayout;
    private SwipeRecyclerView mPullToRefreshListView;

    private WithdrawRecordAdapter adapter;

    private List<WithdrawRecordBean> beanList = new ArrayList<>();

    private int currentPage = 0;
    private int currentPageSize = 50;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw_record);
        initActionBar();
        initView();
        initData();
    }

    private void initActionBar() {
        getSupportActionBar().hide();
        findViewById(R.id.iv_title_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView mTvTitle = (TextView) findViewById(R.id.tv_title_center);
        mTvTitle.setText(getString(R.string.withdraw_record));
    }

    private void initView(){
        mRefreshLayout = findViewById(R.id.refreshLayout);
        mPullToRefreshListView = findViewById(R.id.recyclerView);
        mPullToRefreshListView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new WithdrawRecordAdapter(WithdrawRecordActivity.this);

        mRefreshLayout.setOnRefreshListener(refreshLayout -> {
            currentPage = 0;
            getWithdrawRecordList();
        });
        mRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
            currentPage += 1;
            getWithdrawRecordList();
        });

        mPullToRefreshListView.setAdapter(adapter);
    }

    private void initData() {
        currentPage = 0;
        getWithdrawRecordList();
    }

    private void getWithdrawRecordList(){

        HashMap<String, String> params = new HashMap<String, String>();
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        params.put("pageIndex", String.valueOf(currentPage));
        params.put("pageSize", String.valueOf(currentPageSize));

        HttpUtils.get().url(coreManager.getConfig().BANK_CARD_WITHDRAW_RECORD)
                .params(params)
                .build()
                .execute(new ListCallback<WithdrawRecordBean>(WithdrawRecordBean.class) {

                    @Override
                    public void onResponse(ArrayResult<WithdrawRecordBean> result) {
                        if (result.getResultCode() == 1 && result.getData() != null && result.getData().size() != 0) {
                            beanList = result.getData();
                            if (currentPage == 0) {
                                adapter.setItems(beanList);
                                if (mRefreshLayout != null) {
                                    mRefreshLayout.finishRefresh();
                                }
                            }else {
                                adapter.addItems(beanList);
                            }
                            adapter.notifyDataSetChanged();
                        }
                        if (currentPage != 0 && mRefreshLayout != null){
                            mRefreshLayout.finishLoadMore();
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        if (mRefreshLayout != null) {
                            if (currentPage != 0) {
                                mRefreshLayout.finishLoadMore();
                            }else {
                                mRefreshLayout.finishRefresh();
                            }
                        }
                        ToastUtil.showNetError(mContext);
                    }
                });

    }
}
